import Vue from 'vue'
import Router from 'vue-router'
import nestedRouter from './modules/nested'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
/**
 * 注意: 子菜单仅在路由时出现 children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * 没有权限要求的基页
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '主页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * 动态加载路由
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [
  {
    path: '/site',
    component: Layout,
    alwaysShow: true,
    name: 'Site',
    meta: {
      title: '平台管理',
      icon: 'international',
      roles: ['Site']
    },
    children: [
      {
        path: 'tenant',
        component: () => import('@/views/site/tenant'),
        name: 'tenant',
        meta: {
          title: '租户列表',
          roles: ['Site.Tenant']
        }
      }, {
        path: 'operation',
        component: () => import('@/views/site/operation'),
        name: 'Operation',
        meta: {
          title: '运营平台',
          roles: ['Site.Operation']
        }
      }, {
        path: 'order',
        component: () => import('@/views/site/order'),
        name: 'Order',
        meta: {
          title: '订单管理',
          roles: ['Site.Order']
        }
      }, {
        path: 'project',
        component: () => import('@/views/site/project'),
        name: 'Project',
        meta: {
          title: '产品列表',
          roles: ['Site.Project']
        }
      }
    ]
  }, {
    path: '/system',
    component: Layout,
    alwaysShow: true,
    name: 'System',
    meta: {
      title: '系统管理',
      icon: 'lock',
      roles: ['System']
    },
    children: [
      {
        path: "permission",
        name: "Permission",
        meta: { title: "后台管理" },
        alwaysShow: true,
        component: () => import('@/views/permission/index'),
        children: [
          {
            path: "system-admin",
            name: "SystemAdmin",
            component: () => import('@/views/permission/user'),
            meta: { title: "管理员列表", roles: ['Permission.User'] }
          }, {
            path: "system-admin-log",
            name: "SystemAdminLog",
            component: () => import('@/views/permission/admin-log'),
            meta: { title: "管理员日志", roles: ['Permission.User'] }
          }, {
            path: "system-role",
            name: "SystemAdmin",
            component: () => import('@/views/permission/role'),
            meta: { title: "管理员角色", roles: ['Permission.Role'] }
          }]
      }, {
        path: 'database',
        component: () => import('@/views/system/database'),
        name: 'Database',
        meta: { title: '数据库管理' }
      }, {
        path: 'setting',
        component: () => import('@/views/system/setting'),
        name: 'Setting',
        meta: { title: '系统配置' }
      }
    ]
  }, {
    path: '/gateway',
    component: Layout,
    alwaysShow: true,
    name: 'Gateway',
    meta: {
      title: '门户管理',
      icon: 'theme',
      roles: ['Gateway']
    },
    children: [
      {
        path: 'basics',
        component: () => import('@/views/gateway/basics'),
        name: 'Basics',
        meta: {
          title: '基础配置',
          roles: ['Gateway.Basics']
        }
      }, {
        path: 'notice',
        component: () => import('@/views/gateway/notice'),
        name: 'Notice',
        meta: {
          title: '新闻公告',
          roles: ['Gateway.Notice']
        }
      }, {
        path: 'create',
        component: () => import('@/views/gateway/create'),
        name: 'Create',
        meta: { title: '创建新闻公告', noCache: true, role: [] },
        hidden: true
      }, {
        path: 'edit/:id(\\d+)',
        component: () => import('@/views/gateway/edit'),
        name: 'Edit',
        meta: { title: '编辑新闻公告', noCache: true, activeMenu: '/gateway/notice' },
        hidden: true
      }
    ]
  }, {
    path: '/blog',
    component: Layout,
    alwaysShow: true,
    name: 'Blog',
    meta: {
      title: '博客管理',
      icon: 'theme'
    },
    children: [
      {
        path: '/blog/menu',
        name: 'Menu',
        component: () => import('@/views/blog/menu'),
        meta: {
          title: '博客菜单'
        }
      },
      {
        path: '/article',
        name: 'Article',
        component: () => import('@/views/blog/article/index'),
        meta: {
          title: '文章列表'
        }
      }, {
        path: '/article/create',
        name: 'CreateArticle',
        component: () => import('@/views/blog/article/create'),
        meta: {
          title: '创建文章', noCache: true
        },
        hidden: true
      }, {
        path: '/article/edit/:id(\\d+)',
        name: 'EdtiArticle',
        component: () => import('@/views/blog/article/edit'),
        meta: {
          title: '编辑文章'
        },
        hidden: true
      }
    ]
  }, {
    path: '/log',
    component: Layout,
    alwaysShow: true,
    name: 'Log',
    meta: {
      title: '日志管理',
      icon: 'bug',
      roles: ['Log']
    },
    children: [
      {
        path: '/error',
        component: () => import('@/views/log/error'),
        name: 'ErrorLog',
        meta: { title: '错误日志', roles: ['Log'] }
      }, {
        path: '/trace',
        component: () => import('@/views/log/trace'),
        name: 'TraceLog',
        meta: { title: '跟踪日志', roles: ['Log'] }
      }, {
        path: '/scheduler',
        component: () => import('@/views/log/scheduler'),
        name: 'SchedulerLog',
        meta: { title: '服务日志', roles: ['Log'] }
      }, {
        path: '/client',
        component: () => import('@/views/log/client'),
        name: 'ClientLog',
        meta: { title: '门户日志', roles: ['Log.AuditLog'] }
      }, {
        path: 'filelog',
        component: () => import('@/views/log/file'),
        name: 'File',
        meta: { title: '文件日志', roles: ['Log.File'] }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: 'icon',
    component: Layout,
    component: () => import('@/views/icons/index'),
    name: 'Icons',
    meta: { title: '图标', icon: 'icon', noCache: true }
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true },
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
